<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class ChatSession extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_chat_session';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'updatetime_text',
        'see_type_text',
    ];

    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return is_numeric($value) ? time_tran($value) : $value;
    }

    // 可见状态:0=正常,1=发送人被拉黑(接收人不可见),2=接收人被拉黑(发送人不可见),3=相互拉黑(两方都不可见)
    public function getSeeTypeList()
    {
        return ['0' => __('正常'), '1' => __('发送人被拉黑(接收人不可见)'), '2' => __('接收人被拉黑(发送人不可见)'), '3' => __('相互拉黑(两方都不可见)')];
    }

    public function getSeeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['see_type']) ? $data['see_type'] : '');
        $list = $this->getSeeTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('User', 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function senduser()
    {
        return $this->belongsTo('User', 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function receiveuser()
    {
        return $this->belongsTo('User', 'receive_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function newmsg()
    {

        // return $this->hasMany('ChatMsg', 'session_id', 'id')->field('id,content,chat_type,see_type,session_id')->order('id desc');
        return $this->belongsTo('ChatMsg', 'id', 'session_id')->field('id,content,chat_type,see_type,session_id');
    }
}
